package cn.fbs.passcontroller;

import cn.fbs.service.DownLoadService;
import cn.fbs.service.ImageDataService;
import cn.fbs.service.UploadService;
import cn.fbs.util.FileToPdfUtils;
import cn.fbs.util.WordToPdfUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UpAndDownloadController {

    @Autowired
    private ImageDataService imageDataService;
    @Autowired
    private UploadService uploadService;

    @Autowired
    private DownLoadService downLoadService;

    @ApiOperation("文件下载")
    @ApiImplicitParam(name = "fileUrl", value = "文件相对路径")
    @RequestMapping(value = {"file/fileDown"}, method = {RequestMethod.GET})
    public void fileDown(HttpServletResponse response, String fileUrl) {
        downLoadService.fileDown(response, fileUrl);
    }

    @ApiOperation("上传图像文件")
    @PostMapping("imageData/uploadImageFile")
    public String uploadImageFile(@RequestParam("file") MultipartFile file) {
        try {
            return uploadService.uploadImage(file);
        } catch (IOException e) {
            e.printStackTrace();
            return "500," + e.getMessage();
        }
    }

    @ApiOperation("上传资料文件")
    @PostMapping("imageData/uploadDataFile")
    public String uploadDataFile(@RequestParam("file") MultipartFile file) {
        try {
            return uploadService.uploadData(file);
        } catch (IOException e) {
            e.printStackTrace();
            return "500," + e.getMessage();
        }
    }


    @ApiOperation("上传视频文件")
    @PostMapping("imageData/uploadVideoFile")
    public String uploadVideoFile(@RequestParam("file") MultipartFile file) {
        try {
            return uploadService.uploadVideo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return "500," + e.getMessage();
        }
    }

    @ApiOperation("批量导入")
    @PostMapping("imageData/batchImport")
    public Object batchImport(@RequestParam("files") List<MultipartFile> files) throws IOException, ParseException {
        return imageDataService.batchImport(files);
    }

    @ApiOperation("批量上传")
    @PostMapping("imageData/batchUpload")
    public ArrayList<String> batchUpload(@RequestParam("files") List<MultipartFile> files) throws IOException {
        ArrayList<String> pathList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        for (MultipartFile file : files) {
            String path = "";
            try {
                path = uploadService.uploadImage(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
            pathList.add(path);
        }
        return pathList;
    }


    @ApiOperation("上传资料文件")
    @PostMapping("photoAlbum/uploadPhotoAlbumFile")
    public String uploadPhotoAlbum(@RequestParam("file") MultipartFile file) throws Exception {
        String fileUrl = null;
        //1.上传图册，返回文件路径
        try {
            fileUrl = uploadService.uploadData(file);
            String fileSuf = fileUrl.substring(fileUrl.indexOf(".") + 1);
            String pdfPath = fileUrl.substring(0, fileUrl.indexOf(".")) + ".pdf";
            /**
             * 区分是什么文件类型
             */
            if (fileSuf.equalsIgnoreCase("doc") || fileSuf.equalsIgnoreCase("docx")) {
                WordToPdfUtil.wordToPdf(fileUrl, pdfPath);
            }
            if (fileSuf.equalsIgnoreCase("ppt") || fileSuf.equalsIgnoreCase("pptx")) {
                FileToPdfUtils.pptToWord(fileUrl, pdfPath);
            }
        } catch (Exception e) {
            return "500,文件格式错误!";
        }
        return fileUrl;

    }


}
